package com.itluma.isWOak.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.mapper.AccountMapper;
import com.itluma.isWOak.pojo.Account;
import com.itluma.isWOak.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: isWOak
 * @author: 小陆
 * @create: 2021-04-07 19:37
 **/
@Service
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountMapper accountMapper;

	//显示所有账务账户信息列表信息
	@Override
	public PageInfo<Account> showList(Integer page, Integer pageNum) {

		PageHelper.startPage(page, pageNum);
		//调用AccountMapper中的showList方法
		List<Account> list = accountMapper.showList();
		System.out.println("list:====="+list.toString());
		if (list.size() > 0 && list != null) {
			PageInfo<Account> info = new PageInfo<>(list);
//			System.out.println("总页数：" + info.getPages());
//			System.out.println("总记录数：" + info.getTotal());
//			System.out.println("当前页码：" + info.getPageNum());
//			System.out.println("每一页的大小：" + info.getPageSize());
//			System.out.println("是否是第一页：" + info.isIsFirstPage());
//			System.out.println("是否是最后一页：" + info.isIsLastPage());
			return info;
		}
		return null;
	}

	@Override
	public Account showOneUserDetail(Integer accountId) {
		Account account = accountMapper.showOneUserDetail(accountId);
		if (account != null) {
			return account;
		}
		return null;
	}

	@Override
	public boolean selectByIdcordNo(String IdcordNo) {
		List<Account> accounts = accountMapper.selectByIdcordNo(IdcordNo);
		System.out.println("account:==="+accounts.size());
		if (accounts.size() > 0) {
			return false;
		}
		return true;
	}

	@Override
	public Account insertAccount(Account account) {
	 	Integer returnRow =  accountMapper.insertAccount(account);
		return returnRow>0?account:null;
	}

	@Override
	public boolean updateStatus(String restatus, Integer accountId) {
		Integer status = null;
		String dateTime = null;
		if (restatus.equals("开通")) {
			status = 0;
		}else if (restatus.equals("暂停")){
			status = 1;
			Date date = new Date();
			dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		}
		Integer b = accountMapper.updateStatus(status ,accountId,dateTime);
		System.out.println("b=========: "+b);
		if (b > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Account updateAccount(Integer accountId) {
		Account account = accountMapper.selectByAccountId(accountId);
		if (account != null) {
			return account;
		}
		return null;
	}

	@Override
	public boolean selectPwd(String oldpwd,Integer aId) {
		Account account =  accountMapper.selectPwd(oldpwd,aId);
		if (account != null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateAccountDetail(Account account) {
		Integer row =  accountMapper.updateAccount(account);
		if (row > 0) {
			return true;
		}

		return false;
	}

	@Override
	public boolean deleteAccount(Integer accountId) {
		Date dat = new Date();
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dat);
		Integer row = accountMapper.deleteAccount(accountId,date);
		if (row > 0){
			System.out.println("row:=========="+row);
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
//		System.out.println(sdf);
	}


}
